#!/bin/sh
# https://www.reddit.com/r/bspwm/comments/12ebz1n/move_window_to_center/

if [ $(bspc query --node focused.floating.\!hidden.window.local --nodes | wc -l) -gt 0 ]; then
    floatwininfo=$(bspc query --node focused.floating.\!hidden.window.local --tree)
    floatwinxcoords=$(echo "$floatwininfo" | cut -d ":" -f38 | cut -d "," -f1)
    floatwinycoords=$(echo "$floatwininfo" | cut -d ":" -f39 | cut -d "," -f1)
    floatwinwidth=$(echo "$floatwininfo" | cut -d ":" -f40 | cut -d "," -f1)
    floatwinheight=$(echo "$floatwininfo" | cut -d ":" -f41 | tr -d "}")
    monitorinfo=$(bspc query --monitor focused --tree)
    monitorwidth=$(echo "$monitorinfo" | cut -d ":" -f18 | cut -d "," -f1)
    monitorheight=$(echo "$monitorinfo" | cut -d ":" -f19 | cut -d "," -f1 | tr -d "}")
    bspc node -v $(( ($monitorwidth/2) - $floatwinxcoords - ($floatwinwidth/2) )) $(( ($monitorheight/2) - $floatwinycoords - ($floatwinheight/2) ))
else
    dunstify -a "bspwm" -u normal --replace=102 "tocenter" "No levatating window is currently focused"
fi
